// 平台管理模块路由处理函数

// 引入jwt
const jwt = require('jsonwebtoken')
const secretKey = 'ethink'    // 密钥
const utils = require("../../common/utils")
const server = require("../../server/admin/admin")


// 获取所有菜单
exports.getAllMenu = (req, res) => {
  server.getAllMenu().then(result => {
    return res.send({
      code: 200,
      message: "获取成功",
      data: result
    })
  }).catch(e => {
    return res.cc(e)
  })
}

// 添加菜单
exports.addMenu = (req, res) => {
  const data = req.body
  server.addMenu(data).then(result => {
    return res.cc(result, 200)
  }).catch(e => {
    return res.cc(e)
  })
}

// 修改菜单
exports.updateMenu = (req, res) => {
  const data = req.body
  server.updateMenu(data).then(result => {
    return res.cc(result, 200)
  }).catch(e => {
    return res.cc(e)
  })
}

// 删除菜单
exports.deleteMenu = (req, res) => {
  let id = req.query.id
  let token = req.get('Authorization').split(' ')[1]
  // token解析
  const data = jwt.verify(token, secretKey)
  server.deleteMenu(id, data.role_id).then(result => {
    return res.cc(result, 200)
  }).catch(e => {
    return res.cc(e)
  })
}

// 添加角色
exports.addRole = (req, res) => {
  const data = req.body
  server.addRole(data).then(result => {
    return res.cc(result, 200)
  }).catch(e => {
    return res.cc(e)
  })
}

// 删除角色
exports.deleteRole = (req, res) => {
  let id = req.query.id
  server.deleteRole(id).then(result => {
    return res.cc(result, 200)
  }).catch(e => {
    return res.cc(e)
  })
}

// 修改角色
exports.updateRole = (req, res) => {
  const data = req.body
  server.updateRole(data).then(result => {
    return res.cc(result, 200)
  }).catch(e => {
    return res.cc(e)
  })
}

// 获取角色列表
exports.getRoleList = (req, res) => {
  let pageSize = req.query.pageSize || 20
  let pageNum = req.query.pageNum
  let role_name = req.query.role_name.trim()
  server.getRoleList(pageNum, pageSize, role_name).then(result => {
    return res.send({
      code: 200,
      message: '获取角色列表成功',
      data: result
    })
  }).catch(e => {
    return res.cc(e)
  })
}

// 获取角色菜单
exports.getRolePermission = (req, res) => {
  let role_id = req.query.id
  server.getRolePermission(role_id).then(result => {
    return res.send({
      code: 200,
      message: "获取角色菜单成功",
      data: result
    })
  }).catch(e => {
    return res.cc(e)
  })
}

// 配置角色权限（菜单）
exports.updateRolePermission = (req, res) => {  
  let role_id = req.body.id
  const menu_ids = req.body.menu_ids
  server.updateRolePermission(role_id, menu_ids).then(result => {
    return res.cc(result, 200)
  }).catch(e => {
    return res.cc(e)
  })
}



// 添加用户
exports.addUser = (req, res) => {
  const data = req.body
  server.addUser(data).then(result => {
    return res.cc(result, 200)
  }).catch(e => {
    return res.cc(e)
  })
}

// 删除用户
exports.deleteUser = (req, res) => {
  let id = req.query.id
  server.deleteUser(id).then(result => {
    return res.cc(result, 200)
  }).catch(e => {
    return res.cc(e)
  })
}

// 修改用户
exports.updateUser = (req, res) => {
  const data = req.body
  server.updateUser(data).then(result => {
    return res.cc(result, 200)
  }).catch(e => {
    return res.cc(e)
  })
}

// 重置用户密码
exports.resetPassword = (req, res) => {
  let id = req.params.id
  server.resetPassword(id).then(result => {
    return res.cc(result, 200)
  }).catch(e => {
    return res.cc(e)
  })
}

// 获取用户列表
exports.getUserList = (req, res) => {
  const data = req.query
  server.getUserList(data).then(result => {
    return res.send({
      code: 200,
      message: "获取用户列表成功",
      data: result
    })
  }).catch(e => {
    return res.cc(e)
  })
}